package P136;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author DJ同学
 * @Date 2021/2/11 23:58
 * @Version 1.0
 * @Name  只出现一次的数字
 * @Problem https://leetcode-cn.com/problems/single-number/
 * @Idea hashmap
 */
public class Solution {

    @Test
    public void test01(){
        int i = singleNumber(new int[]{4, 1, 2, 1, 2});
        System.out.println(i);
    }

    public int singleNumber(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                map.put(nums[i],2);
            }else{
                map.put(nums[i],1);
            }
        }
        int res=0;
        for(Integer i:map.keySet()){
            if(map.get(i)==1){
                res=i;
                break;
            }
        }
        return res;
    }
}
